// pages/chat/chat.js
Page({
  data: {
    userId: '',
    userName: '',
    userAvatar: '/images/avatar-default.png',
    otherUserAvatar: '/images/avatar-default.png',
    messageList: [],
    inputText: '',
    scrollTop: 0,
    scrollIntoView: ''
  },

  onLoad(options) {
    const { userId, userName } = options
    
    wx.setNavigationBarTitle({
      title: userName || '聊天'
    })
    
    this.setData({
      userId,
      userName
    })
    
    this.loadChatHistory()
  },

  loadChatHistory() {
    // 模拟加载聊天记录
    const mockMessages = [
      {
        id: 1,
        type: 'text',
        content: '你好，关于代课的事情我想了解一下',
        time: '10:30',
        isSelf: false
      },
      {
        id: 2,
        type: 'text',
        content: '好的，有什么问题可以问我',
        time: '10:31',
        isSelf: true
      },
      {
        id: 3,
        type: 'text',
        content: '具体是什么课程？需要做什么？',
        time: '10:32',
        isSelf: false
      },
      {
        id: 4,
        type: 'text',
        content: '是高等数学课，主要是听课做笔记就可以了',
        time: '10:33',
        isSelf: true
      }
    ]
    
    this.setData({
      messageList: mockMessages
    })
    
    this.scrollToBottom()
  },

  onInputChange(e) {
    this.setData({
      inputText: e.detail.value
    })
  },

  sendMessage() {
    const { inputText } = this.data
    
    if (!inputText.trim()) {
      return
    }
    
    const newMessage = {
      id: Date.now(),
      type: 'text',
      content: inputText.trim(),
      time: this.formatTime(new Date()),
      isSelf: true
    }
    
    this.setData({
      messageList: [...this.data.messageList, newMessage],
      inputText: ''
    })
    
    this.scrollToBottom()
    
    // 模拟对方回复
    setTimeout(() => {
      this.simulateReply()
    }, 1000)
  },

  simulateReply() {
    const replies = [
      '好的，我知道了',
      '没问题',
      '收到',
      '明白了',
      '好的，谢谢'
    ]
    
    const replyMessage = {
      id: Date.now(),
      type: 'text',
      content: replies[Math.floor(Math.random() * replies.length)],
      time: this.formatTime(new Date()),
      isSelf: false
    }
    
    this.setData({
      messageList: [...this.data.messageList, replyMessage]
    })
    
    this.scrollToBottom()
  },

  chooseImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const imagePath = res.tempFilePaths[0]
        
        const imageMessage = {
          id: Date.now(),
          type: 'image',
          content: imagePath,
          time: this.formatTime(new Date()),
          isSelf: true
        }
        
        this.setData({
          messageList: [...this.data.messageList, imageMessage]
        })
        
        this.scrollToBottom()
      }
    })
  },

  previewImage(e) {
    const src = e.currentTarget.dataset.src
    wx.previewImage({
      current: src,
      urls: [src]
    })
  },

  scrollToBottom() {
    setTimeout(() => {
      const lastMessageId = this.data.messageList[this.data.messageList.length - 1]?.id
      if (lastMessageId) {
        this.setData({
          scrollIntoView: `msg-${lastMessageId}`
        })
      }
    }, 100)
  },

  formatTime(date) {
    const hours = date.getHours().toString().padStart(2, '0')
    const minutes = date.getMinutes().toString().padStart(2, '0')
    return `${hours}:${minutes}`
  }
})